package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Hashtable;

import au.edu.unsw.editingtool.global.EditingToolException;


public class DBGroup extends DBManager {

	private void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("GroupID", "gid");
		MappingTable.put("Label", "label");
		MappingTable.put("Note", "note");
	}
	
	public DBGroup() {
		this.table = "usergroup";
		this.primaryKeys = new String[]{"GroupID"}; 
		this.setupDBMapping();
	}
	
	public void addGroup(Hashtable<String, String> group) throws EditingToolException, SQLException, ClassNotFoundException {
		this.setTable(this.table, "");
		this.setColumnValues(group);
		this.initDB();
		this.insert(false, false);
		this.clean();
	}
	
	public String getGroupName(String gid) throws EditingToolException, SQLException, ClassNotFoundException 
	{		
		String groupName = null;
		this.setTable(this.table, "");
		String fields[] = {"Label"};
		
		this.setFieldsCondition("GroupID", this.setEqual(gid));
		this.addField(fields);	
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			System.out.println("[DBGROUP] getGroupName Resultset = NULL");
			this.clean();
			return groupName;
		}
				
		int count = 0;
		while(ResultSet.next())
		{
			count ++;
			groupName = (String) ResultSet.getObject(1);
		}
		
		if (count==1)
		{
			System.out.println("[DBGROUP] getGroupName Resultset = 1");
			this.clean();
			return groupName;
		}
		else
		{
			this.clean();
			return null;
		}
	}
}
